package com.atguigu.service.impl;

import com.atguigu.mapper.EmployeeMapper;
import com.atguigu.pojo.Employee;
import com.atguigu.service.EmployeeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Service
@Transactional
public class EmployeeServiceImpl implements EmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    @Transactional(readOnly = true)
    public List<Employee> findAll() {
        return this.employeeMapper.findAll();
    }

    @Override
    public int saveEmp(Employee employee) {
        return this.employeeMapper.saveEmp(employee);
    }

    @Override
    public int saveEmp2(Employee employee, Employee employee2) {
        int n1 = this.employeeMapper.saveEmp(employee);
        int n2 = this.employeeMapper.saveEmp(employee2);
        return n1+n2;
    }

    @Override
    public PageInfo findByPage(Integer pageNum, Integer pageSize) {
        //启用分页插件（会查询一共有几条记录，会拦截findAll的语句并追加limit子句）
        PageHelper.startPage(pageNum,pageSize);
        //调用数据库查询所有数据，查询出来的却不是所有的数据，而是指定页的数据
        List<Employee> empList = this.employeeMapper.findAll();
        return new PageInfo(empList,12);
    }
}
